#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Defines the common namespace for evaulating url records.

"""
from functools import partial

def connect(dsn):
    try:
        from psycopg2 import connect as _con
    except:
        try:
            #from pymssql import connect as _con
            from pyodbc import connect as _con
        except:
            from odbc import odbc as _con
            return _con(dsn)
        else:
            return _con(dsn) # **mssql_dsn(dsn))
    else:
        return _con(dsn)


def mssql_dsn(dsn):
    d = dict(a.split('=') for a in dsn.split(';'))
    d.pop('Driver')
    d['host'] = d.pop('Server')
    d['user'] = d.pop('uid')
    d['database'] = d.pop('Database')
    d['password'] = d.pop('pwd')
    return d

## lots of reuseable behavior here
from tack.lib import not_found, string_app, shift_app, echo_app, default401
from tack.lib import status_codes, content_headers, template_app
from tack.lib.dbconnector import dbconnector
from tack.lib.dbjson import readrec, readrecs, addrec, modrec, delrec
from tack.lib.fileserver import fileserver
from tack.lib.formreader import formreader
from tack.lib.cookiemonster import cookie_monster, cookie_crumbler
from tack.lib.authz import authz
from tack.lib.authen import authen, akoauthen, basic_authen


confdbcon = partial(dbconnector,
                    source_key='tack.siteconfig',
                    target_key='connection',
                    connect=connect)
